﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using LitJson;
class MessageHandler:SingleTon<MessageHandler>
{
    public void Handler(Socket client,Message message)
    {
        Console.WriteLine(message.msg);
        Send(client, message.type, message.msg);
        switch (message.type)
        { 

        }
    }
    public void ReciveMessage(Socket client, byte[] data, int count)
    {
        string msg = Encoding.UTF8.GetString(data, 0, count);
        JsonData jsonData = JsonMapper.ToObject(msg);
        int jsonType = (int)jsonData[0];
        string jsonMsg = (string)jsonData[1];
        Message message = new Message(jsonType, jsonMsg);
        Handler(client,message);
    }
    public void Send(Socket client,int type, string obj)
    {
        byte[] data = GetMessage(type, obj);
        client.Send(data);
    }
    public byte[] GetMessage(int type, string obj)
    {
        string str = obj.ToString();
        Message message = new Message(type, str);
        string msg = JsonMapper.ToJson(message);
        return Encoding.UTF8.GetBytes(msg);
    }
    public class Message
    {
        public int type;
        public string msg;
        public Message(int type, string msg)
        {
            this.type = type;
            this.msg = msg;
        }
    }
}

